//package com.tbvtc.agriculturalcreditchainserver2025.task;
//
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.tbvtc.agriculturalcreditchainserver2025.entity.CameraCropMapping;
//import com.tbvtc.agriculturalcreditchainserver2025.entity.Crop;
//import com.tbvtc.agriculturalcreditchainserver2025.entity.DailyScore;
//import com.tbvtc.agriculturalcreditchainserver2025.entity.EnvironmentData;
//import com.tbvtc.agriculturalcreditchainserver2025.service.ICameraCropMappingService;
//import com.tbvtc.agriculturalcreditchainserver2025.service.ICropService;
//import com.tbvtc.agriculturalcreditchainserver2025.service.IDailyScoreService;
//import com.tbvtc.agriculturalcreditchainserver2025.service.IEnvironmentDataService;
//import com.tbvtc.agriculturalcreditchainserver2025.utils.PearEnvironmentScorer;
//import com.tbvtc.agriculturalcreditchainserver2025.utils.huawei.HwCloudIot;
//import com.tbvtc.agriculturalcreditchainserver2025.constant.RedisConstant;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//import java.time.LocalDate;
//import java.time.LocalDateTime;
//import java.time.LocalTime;
//import java.util.List;
//import java.util.Map;
//import java.util.Objects;
//import java.util.OptionalDouble;
//import java.util.stream.Collectors;
//
//@Component
//public class EvnScheduled {
//
//    @Resource
//    private IEnvironmentDataService iEnvironmentService;
//    @Resource
//    private ICameraCropMappingService iCameraCropMappingService;
//    @Resource
//    private ICropService cropService;
//    @Resource
//    private HwCloudIot hwCloudIot;
//    @Resource(name = "TredisTemplate")
//    private RedisTemplate<String, Object> redisTemplate;
//    @Resource
//    private PearEnvironmentScorer pearEnvironmentScorer;
//    @Resource
//    private IDailyScoreService dateService;
//
//    public void compareBlockHeights() {
//        List<CameraCropMapping> list = iCameraCropMappingService.list();
//        for (CameraCropMapping cameraCropMapping : list) {
//            //获取到所有的农作物
//            Crop crop = cropService.getById(cameraCropMapping.getCropId());
//            //获取到所有的环境数据
//            //光照
//            try {
//                int gzValue = hwCloudIot.getIlluminationValue();
//                Map<String, Double> temAndHumInfo = hwCloudIot.getTemAndHumInfo();
//                Double temperature = temAndHumInfo.get("temperature");
//                Double humidity = temAndHumInfo.get("humidity");
//                EnvironmentData environmentData = EnvironmentData.builder()
//                        .locationId(cameraCropMapping.getCropId())
//                        .humidity(humidity)
//                        .temperature(temperature)
//                        .illumination(gzValue)
//                        .monitorTime(LocalDateTime.now())
//                        .growthStage(Objects.requireNonNull(redisTemplate.opsForValue().get(
//                                RedisConstant.CROP_STAGE_TYPE + crop.getName())).toString())
//                        .build();
//                iEnvironmentService.save(environmentData);
//            } catch (Exception e) {
//                throw new RuntimeException(e);
//            }
//            //温湿度
//        }
//
//    }
//
//    // 定义7个定时任务，对应每天7次监测（2:00、6:00、10:00、13:00、16:00、19:00、22:00）
//    @Scheduled(cron = "0 0 2 * * ?")  // 每天2:00执行
//    public void saveDataAt2() {
//        compareBlockHeights();
//    }
//
//    @Scheduled(cron = "0 0 6 * * ?")  // 每6:00执行
//    public void saveDataAt6() {
//        compareBlockHeights();
//    }
//
//
//    @Scheduled(cron = "0 0 10 * * ?")  // 每天10:00执行
//    public void saveDataAt1020() {
//        compareBlockHeights();
//
//    }
//
//    @Scheduled(cron = "0 0 14 * * ?")  // 每天14:00执行
//    public void saveDataAt13() {
//        compareBlockHeights();
//    }
//
//    @Scheduled(cron = "0 0 18 * * ?")  // 每天18:00执行
//    public void saveDataAt16() {
//        compareBlockHeights();
//    }
//
//    @Scheduled(cron = "0 0 22 * * ?")  // 每天22:00执行
//    public void saveDataAt19() {
//        compareBlockHeights();
//        //温差
//        Double temperatureDifference = getTemperatureDifference();
//        //光照（一天的平均值）
//        Double illumination = getIllumination();
//        //温度一天平均值
//        Double temperature = getTemperature();
//        //湿度一天平均值
//        Double humidity = getHumidity();
//        double score = pearEnvironmentScorer.calculateScore(temperatureDifference, illumination, temperature, humidity);
//        //储存当天的分数
//        dateService.save(new DailyScore(LocalDate.now(),score));
//    }
//    public Double getHumidity(){
//        List<EnvironmentData> list = getList();
//        OptionalDouble average = list.stream().mapToDouble(EnvironmentData::getHumidity).average();
//        if (average.isPresent()) {
//            return average.getAsDouble();
//        }
//        return 0.0;
//    }
//    public Double getTemperature(){
//        List<EnvironmentData> list = getList();
//        OptionalDouble average = list.stream().mapToDouble(EnvironmentData::getTemperature).average();
//        if (average.isPresent()) {
//            return average.getAsDouble();
//        }
//        return 0.0;
//    }
//
//    public Double getIllumination(){
//        List<EnvironmentData> list = getList();
//// list.stream().map()
//        //光照
//        OptionalDouble average = list.stream().mapToInt(EnvironmentData::getIllumination).average();
//        if (average.isPresent()) {
//            return average.getAsDouble();
//        }
//        return 0.0;
//    }
//
//    public List<EnvironmentData> getList(){
//        LocalDate date = LocalDate.now();
//        LocalDateTime beginTime = date.atTime(3, 0);
//        LocalDateTime endTime = date.atTime(19, 0);
//        return iEnvironmentService.list(new QueryWrapper<EnvironmentData>().
//                between("monitor_time", beginTime, endTime));
//    }
//
//
//    public Double getTemperatureDifference(){
//        //今天时间
//        LocalDate now = LocalDate.now();
//        LocalDateTime beginTime = LocalDateTime.of(now, LocalTime.MIN);
//        LocalDateTime endTime = LocalDateTime.of(now, LocalTime.MAX);
//        List<EnvironmentData> list = iEnvironmentService.list(new QueryWrapper<EnvironmentData>().
//                between("monitor_time", beginTime, endTime));
//        List<Double> temList = list.stream().map(
//                EnvironmentData::getTemperature).collect(Collectors.toList());
//        // 遍历列表找到最大值和最小值
//        double max=0;
//        double min=0;
//        for (Double num : temList) {
//            if (num > max) {
//                max = num;
//            }
//            if (num < min) {
//                min = num;
//            }
//        }
//        double temperatureDifference = max - min;
//        //保留一位
//        return Math.round(temperatureDifference * 10) / 10.0;
//    }
//
//}
